<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-06-29 19:51:21 +0200 (Sun, 29 Jun 2008) $
 * $Rev: 1238 $
 * $Author: gavinspearhead $
 * $Id: admin_log.php 1238 2008-06-29 17:51:21Z gavinspearhead $
 */
define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);

$pathadl = realpath(dirname(__FILE__));


$pathhtmli = realpath(dirname(__FILE__));
require_once "$pathhtmli/../functions/defines.php";
require_once "$pathhtmli/../config.php";
require_once "$pathhtmli/../functions/urdversion.php";

if (isset($config['urdweb_logfile']))
	$config['log_file'] = $config['urdweb_logfile'];
else 
	$config['log_file'] = '/dev/null';
$process_name = 'urd_web'; // needed for message format in syslog and logging 

require_once "$pathhtmli/../functions/functions.php";
require_once "$pathhtmli/../functions/urd_log.php";

// initialise some stuff

try {
	$db = connect_db();  // initialise the database
} catch (exception $e) {
	echo "Connection to database failed.\n";
	die;
}


$prefs = load_config($db); // load the root prefs

// first include all the php files that only define stuff
require_once "$pathhtmli/../functions/exception.php";
require_once "$pathhtmli/../functions/autoincludes.php";
require_once "$pathhtmli/../html/fatal_error.php";

require_once "$pathhtmli/../functions/web_functions.php";

// then execute code we always need
require_once "$pathhtmli/../functions/fix_magic.php";
require_once "$pathhtmli/../urdd/urdd_client.php";

$limit = min (50, get_request('limit', 0));

$groupID = get_request('groupID', FALSE);
$feedid = get_request('feed_id', FALSE);
$minsize = get_request('minsize', 0);
$maxage = get_request('maxage', 0);
$flag = get_request('flag', '');
$userID = get_request('userid', 0);

if ($maxage == '') $maxage = 0;
if ($minsize == '') $minsize = 0;
//if ($groupID == '') $groupID = 0;

function use_groups($type)
{
	return (($type & 2) > 0) ;
}

function use_feeds($type)
{
	return(($type & 1) > 0);
}

if ($groupID === FALSE && $feedid == FALSE) {
	$type = 0; // none
	fatal_error('none selected');
} else if ($groupID === FALSE)
	$type = 1; // rsssets
else if ($feedid === FALSE)
	$type = 2; // groups
else  {
	$type = 3; // both // ???
	$groupID = $feedid = 0;
	fatal_error('both selected');
}

//var_dump($type, ' ',  use_groups($type), use_feeds($type), $groupID, $feedid);

$url = $prefs['url'];

if (!is_numeric ($maxage))
	fatal_error('Maxage must be numeric');
if (!is_numeric ($minsize))
	fatal_error('Minsize must be numeric');
if (!is_numeric ($limit))
	fatal_error('Limit must be numeric');
if (use_feeds($type) && !is_numeric($feedid))
	fatal_error('FeedID must be numeric');
if (use_groups($type) && !is_numeric ($groupID))
	fatal_error('GroupID must be numeric');

$db->escape($groupID, FALSE);
$db->escape($feedid, FALSE);
$db->escape($minsize, FALSE);
$db->escape($maxage, FALSE);
$db->escape($limit, FALSE);

if (use_groups($type) ){
	$group = '';
	if ($groupID != 0) {
		$group = group_name($db, $groupID);
	}
	$title = $group;
}

if (use_feeds($type) ){
	$feed = '';
	if ($feedid != 0) {
		$feed = feed_name($db, $feedid);
	}
	$title = $feed;
}

$build_date = date ('r', time());

$version = get_version();

$rss =<<<RSS
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="{$url}html/rss.php" rel="self" type="application/rss+xml" />
<title>URD $title</title>
<link>http://www.urdland.com</link>
<description>URD recent sets</description>
<generator>URD $version @ $url</generator>
<lastBuildDate>$build_date</lastBuildDate>
<language>en-UK</language>
<ttl>60</ttl>
RSS;

$Qsearch = $Qsize =  $Qage = $Qgroup = $Qfeed = '';

if ($minsize > 0)
	$Qsize = " AND \"size\" > (1024 * 1024 * $minsize) ";

if (use_groups($type)) {
	if ($groupID != 0)
		$Qgroup = " AND \"groupID\" = $groupID ";
}

if (use_feeds($type)) {
	if ($feedid != 0)
		$Qfeed = " AND \"rss_id\" = $feedid ";
}
if ($maxage > 0)
	$Qage = 'AND ('. time() . " - date) / 3600 / 24 <= $maxage ";

$Qflag = '';
if ($flag == 'interesting' && $userID != 0)
	$Qflag = ' AND usersetinfo."statusint"=1 ';

$now = time();

if (use_groups($type)) {
	$sql = "*, (100 * \"binaries\" / GREATEST(1,articlesmax)) AS complete,
		($now - date) AS age
		FROM setdata LEFT JOIN usersetinfo ON (usersetinfo.\"setID\" = setdata.\"ID\") AND (usersetinfo.\"userID\" = $userID) 
		WHERE 1=1 $Qgroup $Qsearch $Qage $Qsize $Qflag
		ORDER BY \"date\" DESC";
} else if (use_feeds($type)) {
	$sql = "setname AS subject, \"setid\" as \"ID\", 100 as \"complete\", \"timestamp\" as \"date\", ($now - timestamp) AS age, \"size\", \"rss_id\"
		FROM rss_sets LEFT JOIN usersetinfo ON (usersetinfo.\"setID\" = rss_sets.\"setid\") AND (usersetinfo.\"userID\" = $userID) 
		WHERE 1=1 $Qfeed $Qsearch $Qage $Qsize $Qflag
		ORDER BY \"timestamp\" DESC";
}

$res = $db->select_query($sql, $limit);
if (!is_array($res))
	die('not found');
else  {
	foreach($res as $row) {
		$subject = $row['subject'];
		$subject = preg_replace('/(\byEnc\b)|(\breq:)|(\bwww\.[\w\.]*\b)|(\bhttp:\/\/[\w\.]*\b)/i', '', $subject);

		$sub_len = strlen($subject);
		if ($sub_len > 60) 
			$sub_short = substr($subject, 0, 57) . '...';
		else
			$sub_short = $subject;
		$sub_short = htmlspecialchars($sub_short);
		$subject = htmlspecialchars($subject);
		$setid = $row['ID'];
		$size = $row['size'];
		$date = date ('r', $row['date']);
		$complete = round($row['complete']);
		$size = round($row['size'] / 1024 / 1024); 
		if (use_groups($type)) {
			if ($groupID == 0) {
				$groupID2 = $row['groupID']; 
				$qry = "\"name\" FROM groups WHERE \"ID\" = '$groupID2' ";
				$r2 = $db->select_query($qry,1);
				if ($r2 === FALSE)
					$group_name = '';
				else 
					$group_name = "Group: {$r2[0]['name']}<br/>";
			} else
				$group_name = "Group: $group<br/>";
			$url_prefix = 'html/browse.php';
		}
		if (use_feeds($type)) {
			if ($feedid == 0) {
				$feedid2 = $row['rss_id']; 
				$qry = "\"name\" FROM rss_urls WHERE \"id\" = '$feedid2' ";
				$r2 = $db->select_query($qry,1);
				if ($r2 === FALSE)
					$group_name = '';
				else 
					$group_name = "Feed: {$r2[0]['name']}<br/>";
			} else
				$group_name = "Feed: $feed<br/>";
			$url_prefix = 'html/rsssets.php';
		}
		$rss .= <<<RSS

<item>
<title>$sub_short</title>
<link>{$url}{$url_prefix}?setid=$setid</link>
<guid>{$url}{$url_prefix}?setid=$setid</guid>
<pubDate>$date</pubDate>
<description><![CDATA[$subject <br/>
$group_name
$size MB<br/>
$complete % complete]]>
</description>
</item>
RSS;
	}

}
$rss .=<<<RSS
</channel>
</rss>


RSS;

echo $rss;

?>

